package com.aifeng.hassan_food.mine

import android.os.Bundle
import android.text.InputFilter
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.base.oneClick
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.SettingViewModel
import kotlinx.android.synthetic.main.activity_real.*
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class RealActivity : BaseActivity() {

    private var isOpen = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_real)
        initTitle(title = "实名认证")
    }

    override fun getViewModel() = getViewModel<SettingViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        real_name.filters = arrayOf<InputFilter>(NameLengthFilter(16))
        real_pwd.transformationMethod = PasswordTransformationMethod.getInstance()

        real_eye.oneClick(duration = 300) {
            if (isOpen) {
                real_pwd.transformationMethod = PasswordTransformationMethod.getInstance()
                real_eye.setImageResource(R.mipmap.icon_close)
                isOpen = false
            } else {
                real_pwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
                real_eye.setImageResource(R.mipmap.icon_open)
                isOpen = true
            }
            real_pwd.setSelection(real_pwd.text.length)
        }
    }

    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_sure -> {
                when {
                    real_name.text.isBlank() -> {
                        real_name.requestFocus()
                        toast("请输入真实姓名")
                        return
                    }
                    real_card.text.isBlank() -> {
                        real_card.requestFocus()
                        toast("请输入身份证号码")
                        return
                    }
                    real_pwd.text.isEmpty() -> {
                        real_pwd.requestFocus()
                        toast("请输入支付密码")
                        return
                    }
                    !CommonUtil.IDCardValidate(real_card.textString()) -> {
                        real_card.requestFocus()
                        toast("请输入正确的身份证号码")
                        return
                    }
                    real_pwd.text.length < 6 -> {
                        toast("请输入6位数字支付密码")
                        return
                    }
                }

                getViewModel().identity(
                    "userid" to getString("userId"),
                    "realname" to real_name.textString(),
                    "idno" to real_card.toUpperCase(),
                    "paymentps" to real_pwd.textString()
                )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            toast("实名认证提交成功！")
                            putShared("realName", real_name.textString())
                            ActivityStack.screenManager.popActivities(this::class.java)
                        }
                    )
            }
        }
    }

}
